﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CloudAppMonitor.Data;

namespace CloudAppMonitor
{
    internal partial class AddCounter : Form
    {
        SqlStorage _storage;

        public Counter SelectedCounter
        {
            get
            {
                return (Counter)Counter.SelectedItem;
            }
        }

        public RoleInstance SelectedRoleInstance
        {
            get
            {
                return (RoleInstance)RoleInstance.SelectedItem;
            }
        }

        public AddCounter(SqlStorage storage)
        {
            _storage = storage;

            InitializeComponent();
        }

        private void AddCounter_Load(object sender, EventArgs e)
        {
            LoadDeployments();
        }

        private void LoadDeployments()
        {
            Deployment.DataSource = _storage.Deployments;
        }

        private void Deployment_SelectedIndexChanged(object sender, EventArgs e)
        {
            Role.DataSource = (from r in _storage.Roles
                               where r.DeploymentId == ((Deployment)Deployment.SelectedItem).DeploymentId
                               select r).ToList();
        }

        private void Role_SelectedIndexChanged(object sender, EventArgs e)
        {
            RoleInstance.DataSource = (from ri in _storage.RoleInstances
                                       where ri.RoleId == ((Role)Role.SelectedItem).RoleId
                                       select ri).ToList();
        }

        private void RoleInstance_SelectedIndexChanged(object sender, EventArgs e)
        {
            Counter.DataSource = _storage.GetCountersForRoleInstance(((RoleInstance)RoleInstance.SelectedItem).RoleInstanceId);
        }
    }
}
